package cn.com.kh;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GetMailContent {
    private static final Log log = LogFactory.getLog(GetMailContent.class);

    public static String getNotifyContent(String titlePart, String contentPart, String mailTime) {

        String sourceContent = readfile(getProperty.get("mailNotifyPath"));
        String targetContent = sourceContent.replace("@{title}", titlePart).replace("@{content}", contentPart).replace("@{mailTime}", mailTime);
        return targetContent;

    }

    public static String getListContent(String titlePart, String contentPart, List<Map<String, Object>> taskList, String mailTime) {

        String sourceContent = readfile(getProperty.get("mailListPath"));
        String targetContent = sourceContent.replace("@{title}", titlePart).replace("@{content}", contentPart).replace("@{mailTime}", mailTime);

        // 定义发送结果集
        Map<String, Object> receviceMap = new HashMap<String, Object>();

        // 定义接收参数
        String trClass = null;
        String taskNo = null;
        String projectName = null;
        String staffName = null;
        String createTime = null;
        String type = null;
        String summary = null;
        String eliteStatus = null;
        String finishTime = null;
        List statusList = new ArrayList();

        String trString =
                "<tr@{class}>" + "    <td>taskNo</td>" + "    <td>projectName</td>" + "    <td>staffName</td>" + "    <td>createTime</td>" + "    <td>type</td>" + "    <td style=\"text-align: center;\">summary</td>" + "    <td>eliteStatus</td>" + "    <td>finishTime</td>" + "    <td style=\"text-align: center;\">statusList</td>" + "</tr>";

        log.debug("开始循环拼接列表,总任务数量:" + taskList.size());
        StringBuffer contentList = new StringBuffer();
        for (int i = 0; i < taskList.size(); i++) {
            // 获取任务
            receviceMap = taskList.get(i);

            // 将当前任务解析拆解
            taskNo = receviceMap.get("taskNo").toString();
            projectName = receviceMap.get("projectName").toString();
            staffName = receviceMap.get("staffName").toString();
            createTime = receviceMap.get("createTime").toString();
            type = receviceMap.get("type").toString();
            summary = receviceMap.get("summary").toString();
            eliteStatus = receviceMap.get("eliteStatus").toString();
            finishTime = receviceMap.get("finishTime").toString();
            statusList = (List<?>) receviceMap.get("statusList");

            // 处理当前任务状态List
            StringBuffer statusPart = new StringBuffer();
            for (int j = 0; j < statusList.size(); ++j) {

                String statusName = statusList.get(j).toString();
                if (KHUtil.isNull(statusName)) {
                    statusName = "0";
                }

                if (Integer.parseInt(statusName) >= 80) {
                    statusPart.append("<a class=\"green-xt\">" + statusName + "%</a>");

                } else if (Integer.parseInt(statusName) >= 60) {
                    statusPart.append("<a class=\"orange-xt\">" + statusName + "%</a>");

                } else if (Integer.parseInt(statusName) >= 30) {
                    statusPart.append("<a class=\"blue-xt\">" + statusName + "%</a>");

                } else {
                    statusPart.append("<a class=\"red-xt\">" + statusName + "%</a>");

                }
            }

            if (i % 2 == 1) {
                trClass = " class=\"xt-bg\"";
            } else {
                trClass = "";
            }

            // 将模板中的内容替换
            contentList.append(trString.replace("@{class}", trClass).replace("taskNo", taskNo).replace("projectName", projectName).replace("staffName", staffName).replace("createTime", createTime).replace("type", type).replace("summary", summary).replace("eliteStatus", eliteStatus).replace("finishTime", finishTime).replace("statusList", statusPart.toString()));
        }

        targetContent = targetContent.replace("@{contentList}", contentList.toString());

        return targetContent;

    }

    public static String readfile(String filePath) {
        log.debug("开始读取模板路径:" + filePath);
        File file = new File(filePath);
        FileInputStream fis = null;
        InputStreamReader reader = null;
        BufferedReader br = null;

        StringBuffer buffer = new StringBuffer();

        try {
            fis = new FileInputStream(file);
            reader = new InputStreamReader(fis, "UTF-8");
            br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }

        } catch (Exception e) {
            log.error("读取模板文件异常！", e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    log.warn("关闭文件BufferedReader Exception！");
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    log.warn("关闭文件InputStreamReader Exception！");
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {
                    log.warn("关闭文件FileInputStream Exception！");
                }
            }
        }

        log.debug("读取邮件模板内容:" + buffer.toString());

        return buffer.toString();
    }

}
